package cn.tlb.xutilsdownload.design;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.tlb.xutilsdownload.R;

@ContentView(R.layout.activity_main)
public class TabLayout_ViewpagerActivity extends AppCompatActivity {
    @ViewInject(R.id.pager)
    private ViewPager viewPager;

    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;

    @ViewInject(R.id.tabs)
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解注入
        //设置支持的ActionBar, FragmentActivity默认支持ActionBar
        //因此, 使用该方法是需要将系统的ActionBar屏蔽
        //已在清单文件中设置
        setSupportActionBar(toolbar);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public Fragment getItem(int position) {
                return ExampleFragment.getExampleFragment("tab"+(position+1));
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "tab"+(position+1);
            }
        });
        //设置Tablayout和Viewpager联动
        tabLayout.setupWithViewPager(viewPager);
    }
}
